<?php

namespace App\Http\Controllers\Front;

use App\Model\Message;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/**
 * Class MessageController
 * @package App\Http\Controllers\Front
 */
class MessageController extends Controller
{
    private $user;

    public function __construct ()
    {
        $this->user = \Auth::user();
    }

    public function getTitle($title)
    {
        $array = [
            "ENTERPRISE_INVITE_STAFF" => "企业消息",
        ];
        return $array[$title];
    }
    /**
     * 获取消息列表
     * @return \Illuminate\Http\Response
     */
    public function getMessageList()
    {
        $messages =  (new \App\Model\Message)->where("to","=", $this->user->id)->orderBy("created_at", "desc")->orderBy("is_read","desc")->get();
        $data = [];
        foreach ($messages as $message){
            $item =[
                'id' => $message->id,
                'title' => "企业消息",
                'is_read' => $message->is_read,
                'is_handle' => $message->is_handle,
                'content' =>$message->content,
                'created_at' => $message->created_at->format('Y-m-d H:i:s')
            ];
            array_push ($data, $item);
        }
        return $this->renderJson ($messages);
    }

    public function getMessageListById($id)
    {
        $message = Message::whereId($id)->first();
        $message->is_read = 1;
        $message->save();
        return $this->renderJson ($message);
    }

    /**
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function setInviteStaff( Request $request)
    {
        $m = Message::whereId($request->input("id"))->first();
        $is_agree = $request->input("is_agree");
        $message = new Message();
        $message->from= $m->to;
        $message->to =  $m->from;
        $message->target_id = $m->from;
        $message->title = "会员消息";
        switch ($is_agree){
            case 1:
                $message->content = "手机号码为：".$this->user->phone."同意加入";
                break;
            case 2:
                $message->content = "手机号码为：".$this->user->phone."不同意意加入";
                break;
        }
        try{
            \DB::beginTransaction();
            $m->is_read = 1;
            $m->is_handle = 1;
            $m->save();
            // 员工同意
            if ($is_agree == 1){
                // todo 查找企业用户
                \DB::table($m->target_type)->update (["is_agree" => 1]);
                $this->user->enterprise_user_id = $m->from;
                $this->user->save();
            }else{
                \DB::table($m->target_type)->where("id","=",$m->target_id)->delete();
            }
            $message->save();
            \DB::commit ();
        }catch (\Exception $exception){
            \DB::rollBack();
            \Log::error ($exception);
            return $this->error ("123");
        }
       return $this->success ();
    }

    /**
     * 获取个数
     * @return \Illuminate\Http\Response
     */
    public function getMessageNum()
    {
        $num = Message::where("target_id", "=",$this->user->id)
            ->where("is_read", 0)
            ->count();
        return $this->success (["num" => $num]);
    }

}
